#region

using Gurock.SmartInspect;
using Microsoft.Practices.Unity.InterceptionExtension;

#endregion

namespace SmartInspect.Extensions.Unity
{
    public class SiExceptionHandler : SiBaseHandler
    {
        private Session _session;

        public SiExceptionHandler()
        {
            Order = 0;
        }

        public override IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            _session = input.Target.GetLogger();
            StartCallHandlerInvoke(input, _session);
            IMethodReturn ret = getNext().Invoke(input, getNext);
            if(ret.Exception != null)
            {
                LogArgs(FormatTitle(input.MethodBase), input, _session, LogEntryType.VariableValue);
                _session.LogException(ret.Exception);
                ret = input.CreateExceptionMethodReturn(ret.Exception);
            }
            StopCallHandlerInvoke(input, _session);
            return ret;
        }
    }
}